package team.knowers.qzh.qzhserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhserver.common.database.BaseDO;
import team.knowers.qzh.qzhserver.dao.entity.CourseEvaluationDO;
import team.knowers.qzh.qzhserver.dao.entity.QuestionAnswerDO;
import team.knowers.qzh.qzhserver.dao.entity.UserDO;
import team.knowers.qzh.qzhserver.dao.mapper.QuestionAnswerMapper;
import team.knowers.qzh.qzhserver.dto.req.PublicAnswerReqDTO;
import team.knowers.qzh.qzhserver.dto.req.PublicQuestionReqDTO;
import team.knowers.qzh.qzhserver.dto.req.QuestionPageReqDTO;
import team.knowers.qzh.qzhserver.dto.resp.AnswerRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.EvaluationPageRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.HomeAnswerRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.QuestionPageRespDTO;
import team.knowers.qzh.qzhserver.service.QuestionAnswerService;
import team.knowers.qzh.qzhserver.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xtg
 */
@Service
@RequiredArgsConstructor
public class QuestionAnswerServiceImpl extends ServiceImpl<QuestionAnswerMapper, QuestionAnswerDO> implements QuestionAnswerService {
    private final UserService userService;

    @Override
    public Boolean publicQuestion(PublicQuestionReqDTO reqDTO) {
        QuestionAnswerDO bean = BeanUtil.toBean(reqDTO, QuestionAnswerDO.class);
        bean.setReplyType(0);
        UserDO byId = userService.getById(reqDTO.getUserId());
        bean.setUserAvatar(byId.getAvatar());
        bean.setUserNickname(byId.getNickname());
        baseMapper.insert(bean);
        return true;
    }

    @Override
    public Boolean publicAnswer(PublicAnswerReqDTO reqDTO) {


        UserDO byId = userService.getById(reqDTO.getUserId());
        QuestionAnswerDO bean = BeanUtil.toBean(reqDTO, QuestionAnswerDO.class);
        bean.setUserAvatar(byId.getAvatar());
        bean.setReplyType(1);
        bean.setUserNickname(byId.getNickname());
        baseMapper.insert(bean);
        return true;
    }

    @Override
    public IPage<QuestionPageRespDTO> pageQuestion(QuestionPageReqDTO requestParam) {
        LambdaQueryWrapper<QuestionAnswerDO> queryWrapper = Wrappers.lambdaQuery(QuestionAnswerDO.class).eq(QuestionAnswerDO::getParaId, requestParam.getParaId())
                .eq(QuestionAnswerDO::getDelFlag, 0)
                .eq(QuestionAnswerDO::getReplyType, 0)
                .orderByDesc(QuestionAnswerDO::getCreateTime);
        IPage<QuestionAnswerDO> resultPage = baseMapper.selectPage(requestParam, queryWrapper);
        return resultPage.convert(each -> BeanUtil.toBean(each, QuestionPageRespDTO.class));
    }

    @Override
    public List<AnswerRespDTO> allAnswer(Long id) {
        LambdaQueryWrapper<QuestionAnswerDO> questionAnswerDOLambdaQueryWrapper = Wrappers.lambdaQuery(QuestionAnswerDO.class).eq(QuestionAnswerDO::getReplyer, id).eq(QuestionAnswerDO::getReplyType, 1).eq(QuestionAnswerDO::getDelFlag, 0).orderByDesc(QuestionAnswerDO::getCreateTime);
        List<QuestionAnswerDO> questionAnswerDOS = baseMapper.selectList(questionAnswerDOLambdaQueryWrapper);
        List<AnswerRespDTO> results = new ArrayList<>();
        for (QuestionAnswerDO answerDO : questionAnswerDOS) {
            results.add(BeanUtil.toBean(answerDO, AnswerRespDTO.class));
        }
        return results;
    }

    @Override
    public List<HomeAnswerRespDTO> homeUserAnswerByTimeDesc(Long userId) {
        LambdaQueryWrapper<QuestionAnswerDO> questionAnswerDOLambdaQueryWrapper = Wrappers.lambdaQuery(QuestionAnswerDO.class).eq(QuestionAnswerDO::getDelFlag, 0).eq(QuestionAnswerDO::getUserId, userId).orderByDesc(QuestionAnswerDO::getCreateTime);
        List<QuestionAnswerDO> questionAnswerDOS = baseMapper.selectList(questionAnswerDOLambdaQueryWrapper);
        List<HomeAnswerRespDTO> results = new ArrayList<>();
        for (QuestionAnswerDO each : questionAnswerDOS) {
            results.add(BeanUtil.toBean(each, HomeAnswerRespDTO.class));
        }
        return results;
    }
}
